layui.define(['element', 'api', 'schema', 'fast_config'], function (exports) {
    "use strict";




    /**
     * Button component
     * */
    var MOD_NAME = 'utils',
        api = layui.api,
        fast_config = layui.fast_config,
        schema = layui.schema,
        $ = layui.jquery,
        treeNodeData,
        globalEnv;


    Date.prototype.format = function (format) {
        var args = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
        };
        if (/(y+)/.test(format))
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var i in args) {
            var n = args[i];
            if (new RegExp("(" + i + ")").test(format))
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
        }
        return format;
    };

    String.prototype.format = function () {
        if (arguments.length == 0) return this;
        var obj = utils.getGlobalEnv(arguments[0]);
        var s = this;
        for (var key in obj) {
            var value = JSON.stringify(obj[key]);
            if (typeof (obj[key]) == 'object') {
                s = s.replace(new RegExp("\"\\$\\{" + key + "\\}\"", "g"), value);

                var sobj = obj[key];
                for (var sk in sobj) {
                    s = s.replace(new RegExp("\\$\\{" + key + "\\." + sk + "\\}", "g"), sobj[sk]);
                }
            }
            s = s.replace(new RegExp("\\$\\{" + key + "\\}", "g"), obj[key]);
        }
        return s;
    }

    var utils = {
        /**
         * 获取服务环境变量
         */

        setGlobalEnv: function (appId) {
            // if(!globalEnv && fast_config.enableEnv){
            if (!globalEnv && fast_config.enableEnv) {
                api.get({
                    enableRootPath: false,
                    url: envPath + "/api/v1/env/getAppEnvironment?appId=" + appId,
                    async: false,
                    success: function (data) {
                        globalEnv = data
                        schema['env'] = data;
                    }
                });
            }
        },
        getGlobalEnv: function (obj) {

            let data = obj;

            // if(obj != undefined && obj.mid != undefined){
            // let amisData={};
            // amisData["mid"] = obj.mid;
            // amisData["type"] = obj.type;
            //
            // data = amisData;
            // }

            let env = schema['env'];
            data = {
                ...data,
                ...env
            }
            return data;
        },
        contentDomIsExist: function (elem) {
            var bool = false;
            if ($(elem).html()) {
                bool = true
            }
            return bool
        },
        stringformat: function (str, data) {
            return str.format(data);
        },
        setTreeNodeData: function (data) {
            treeNodeData = data;
        },
        getTreeNodeData: function () {
            return treeNodeData;
        },


        getTabElem: function (id) {
            var elem;
            var domId = "treenode_" + id;
            elem = '#' + domId;

            return elem;
        },

        formatSchema: function (schema, data) {
            var postFormatSchema = JSON.parse(utils.stringformat(JSON.stringify(schema), data));
            return postFormatSchema
        },

        //执行表达式
        evalExpression: function (expression, data) {
            var {
                ...__
            } = data;
            let str = expression
            const reg = /\$\{(.+?)\}/;
            const result = str.match(reg);

            if (result.length == 2) {
                str = result[1];
                try {
                    return eval(str);
                } catch (e) {
                    return false;
                }
            } else {
                return false;
            }
        },
        // 通过激活的id缓存当前的node数据
        setTreeNode: function (nodeTId) {

            var currentTreeId = sessionStorage.getItem("content-pear-tab-data-current")
            currentTreeId = currentTreeId.slice(0,currentTreeId.indexOf('_'))
            // 设置定时器, 异步获取根节点, 
            setTimeout(() => {
                var treeNode = $.fn.zTree.getZTreeObj(currentTreeId).getNodeByTId(nodeTId)
                sessionStorage.setItem('content-pear-current-treeNode', JSON.stringify(treeNode))
            }, 500)
            // var treeNode = $.fn.zTree.getZTreeObj(currentTreeId).getNodeByTId(nodeTId)
            // sessionStorage.setItem('content-pear-current-treeNode', JSON.stringify(treeNode))
        },
        contextData: {
            contextmenuData: {},
            trees_settings: {},
            currentTreeId: null
        },

        schemas: schema.schems,

        treeConst: null,

        callback: null
    }


    exports(MOD_NAME, utils);
});